/**
 * 登录页面
 */
var url = self.location.href.split("#");
if (url.length > 1) {
	self.location.href = url[0];
}
var v_getBack_reset = new Vue({
	el: '#resetPasswordPage',
	data: {
		title: '重置密码',
		needBack: true,
		needRefresh: false,
		show: false,
		form: {
			newPwd: '',
			newPwd2: ''
		}
	},
	methods: {
		goback: function() {
			
		},
		postChange: function() {
			var param = $.extend({}, v_getBack_reset.form, v_getBack_checkMobile.form);
			axios.post('/agent/reset/password', param)
			.then(function(resp) {
				if (resp.data.success) {
					$.toast('密码已更新，请重新登录');
					setTimeout(function() {
						self.location.reload();
					}, 1000);
				}
			})
			.catch(function (error) {
				console.log(error);
			});
		}
	}
});

var v_getBack_checkMobile = new Vue({
	el: '#checkMobilePage',
	data: {
		title: '输入手机号码',
		needBack: true,
		needRefresh: false,
		show: false,
		form: {
			mobileNo: '',
			captchaCode: ''
		}
	},
	methods: {
		goback: function() {
			
		},
		sendCode: function() {
			var mobile = this.form.mobileNo;
			if (mobile) {
				var $modal = $.modal({
					title:  '发送短信验证码',
					text:$('#sendCodeDiv').html(),
					buttons: [{
				    	text: '取消',
				    	close: true
					}, {
				    	text: '发送',
				    	bold: true,
				    	close: false,
				    	onClick: function() {
				    		var code = $('div.modal.modal-in').find('input').val();
				    		axios.post('/agent/send/sms', {mobileNo: mobile, code: code, captchaName: 'user_forgot_code'})
							.then(function(resp) {
								if (resp.data.success) {
									$.toast("短信已发送成功，5分钟内有效");
									$.closeModal($modal);
								} else {
									$.toast(resp.data.message);
								}
							})
							.catch(function (error) {
								console.log(error);
							});
				    	}
				    }]
				});
				$('div.modal.modal-in').find('img').off('click').on('click', function() {
					v_getBack_checkMobile.changeImageCaptcha();
				});
				v_getBack_checkMobile.changeImageCaptcha();
			} else {
				 $.alert('请输入手机号码', '警告');
			}
		},
		changeImageCaptcha: function() {
			$('div.modal.modal-in').find('img').attr('src', '/captcha/get/user_forgot_code?' + new Date().getTime());
		},
		checkCaptcha: function() {
			if (this.form.captchaCode) {
				axios.post('/agent/check/mobile/code', {mobileNo: this.form.mobileNo, captchaCode: this.form.captchaCode})
				.then(function(resp) {
					if (resp.data.success) {
						v_getBack_reset.show = true;
						v_getBack_reset.$nextTick(function() {
							$.router.load('#resetPasswordPage');
						});
					} else {
						$.toast(resp.data.message);
					}
				})
				.catch(function (error) {
					console.log(error);
				});
			} else {
				$.alert('请输入短信验证码', '警告');
			}
		}
	}
});

var v_login = new Vue({
	el: '#loginPage',
	data: {
		title: '',
		needBack: false,
		needRefresh: false,
		form: {
			username: '',
			password: '',
			rememberme: true
		}
	},
	methods: {
		gotoRigist: function() {
			self.location.href = '/recommend/b59d7b74e427cd17a9ecb13566f74a75';
		},
		gotoForgot: function() {
			v_getBack_checkMobile.show = true;
			v_getBack_checkMobile.$nextTick(function() {
				$.router.load('#checkMobilePage');
			});
		},
		validate: function() {
			var valid = true;
			if (!this.form.username) {
				valid = false;
				$.toast('请输入用户名');
			} else if (!this.form.password) {
				valid = false;
				$.toast('请输入密码');
			}
			return valid;
		},
		login: function() {
			if (this.validate()) {
				$.post('/login', this.form, function(resp) {
					if (resp.success) {
						$.toast('登录成功！');
						self.location.href = resp.message.url;
					} else {
						$.toast(resp.message);
						v_login.form.password = '';
					}
				});
			}
		}
	}
});